package cn.kgc.tangcco.tcbd1011.bigredbook.tbuser.action;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.kgc.tangcco.tcbd1011.bigredbook.pageresult.PageResult;
import cn.kgc.tangcco.tcbd1011.bigredbook.tbuser.entity.Tbuser;
import cn.kgc.tangcco.tcbd1011.bigredbook.tbuser.service.UserService;
import cn.kgc.tangcco.tcbd1011.bigredbook.util.Utils;
import cn.kgc.tangcco.tcbd1011.weihuihui.comm.base.BaseServlet;
import cn.kgc.tangcco.tcbd1011.weihuihui.comm.properties.factory.PropertiesFactory;
import cn.kgc.tangcco.tcbd1011.weihuihui.comm.util.MybeanUtils;


@WebServlet(name="UserServlet",urlPatterns="/user.action")
public class UserServlet extends BaseServlet {

	private UserService uservice;
	public UserServlet() {
		uservice=(UserService) PropertiesFactory.getValue(Utils.TBUSERSERVICE);
	}
	public static final String SUCCESS="user.action?methodName=doListUser&";
	public String doListUser(HttpServletRequest request, HttpServletResponse response) {
		String forward = "user/listUser";
		String uname=request.getParameter("username");
		int userroles=Utils.parsetInt(request.getParameter("userroles"));
		Tbuser user=new Tbuser();
		if(uname!=null&&!uname.isEmpty()) {
			user.setUsername(uname);
		}
		user.setUserroles(userroles);
		PageResult pages=new PageResult();
		int pageNumber = 1;
		String strPageNumber = request.getParameter("pageNumber");
		if (strPageNumber != null && !strPageNumber.isEmpty()) {
			pageNumber = Integer.parseInt(strPageNumber);
		}
		pages.setPageNumber(pageNumber);
		 int pageSize = cn.kgc.tangcco.tcbd1011.bigredbook.pageresult.PageUtil.getPageSize();
		 pages.setPageSize(pageSize);
		 int totalPosts = this.uservice.queryCountUser(user);
		 pages.setTotalPosts(totalPosts);
		//总页面数
		int totalPages =cn.kgc.tangcco.tcbd1011.bigredbook.pageresult.PageUtil.getTotalPages(totalPosts, pageSize);
		pages.setTotalPages(totalPages);
		request.setAttribute("page", pages);
		List<Tbuser> ulist = this.uservice.queryAllUser(user, pages);
		request.setAttribute("ulist", ulist);
		return forward;
	}
	
	//修改
	public String toUpdateUser(HttpServletRequest request, HttpServletResponse response) {
		String forward = "user/updateUser";
		request.setAttribute("user", this.uservice.detailUser(Utils.parsetInt(request.getParameter("userid"))));
		return forward;
	}
	public String doUpdateUser(HttpServletRequest request, HttpServletResponse response) {
		int urows=this.uservice.updateUser(MybeanUtils.populate(Tbuser.class, request.getParameterMap()));
		String forward = "";
		if(urows>0) {
			forward=SUCCESS+"urows="+urows;
		}else {
			forward="user.action?methodName=toUpdateUser";
		}
		return forward;
	}
	
	//ajax删除
	public String ajaxDeleteUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String forward = "jsonforward";
		int pk=Utils.parsetInt(request.getParameter("userid"));
		int rows=this.uservice.deleteUser(pk);
		String result = JSON.toJSONString(rows);
		PrintWriter out = response.getWriter();
		out.println(result);
		out.flush();
		out.close();
		return forward;
	}
	

}
